<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:log="livcos.org/ns/pm/log"
  xmlns:pm="livcos.org/ns/pm"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns="http://www.w3.org/1999/xhtml"
  exclude-result-prefixes="log pm xs">
  
  <xsl:import href="../../style/base.xsl" />
	
	<xsl:variable name="testTodoUri" select="'/livcos.org/WorkLog/data/ToDo'"/>

  <xsl:template match="/">
    <html id="{$viewObjId}">
      <head>
        <title>Punch Log</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <meta name="served" content="{current-dateTime()}"/>
      	<link href="{$toRootPath}/livcos.org/web/css/global.css" rel="stylesheet" type="text/css" />
      	<link href="{$toRootPath}/livcos.org/web/css/web.css" rel="stylesheet" type="text/css" />
      	<link href="{$toRootPath}/livcos.org/web/css/doc.css" rel="stylesheet" type="text/css" />
      	<script type="text/javascript" src="{$toRootPath}/livcos.org/web/script/util.js"><!-- --></script>
      	<script type="text/javascript" src="{$toRootPath}/livcos.org/web/script/E.js"><!-- --></script>
      	<script type="text/javascript" src="{$toRootPath}/livcos.org/web/script/init.js"><!-- --></script>
      	<script type="text/javascript" src="{$toRootPath}/livcos.org/web/script/gui.js"><!-- --></script>
      	<script type="text/javascript" src="script/punch.js"><!-- --></script>
      </head>
      <body onload="punchConnect()" onunload="punchDisconnect()">
        <table border="0" style="width:100%">
          <tr>
            <td style="font-size: 80%">
              <div id="log"/>
            </td>
            <td style="text-align: right">
              <form>
                <xsl:apply-templates
                  select="document($testTodoUri)/*//pm:task" mode="list"/>
                <input type="text" name="ref" id="ref" size="60"
                  value="{$testTodoUri}#task_2"/>
                <br/>
                <input type="text" name="time" size="36"/>
                <xsl:text> </xsl:text>
                <input type="button" value="now"
                  onclick="this.form.elements['time'].value = (new Date());"/>
                <xsl:text> </xsl:text>
                <input type="button" value="10min"
                  onclick="this.form.elements['time'].value = (new Date(Date.now()-10*60*1000));"/>
                <xsl:text> </xsl:text>
                <input type="button" value="30min"
                  onclick="this.form.elements['time'].value = (new Date(Date.now()-30*60*1000));"/>
                <xsl:text> </xsl:text>
                <input type="button" value="1h"
                  onclick="this.form.elements['time'].value = (new Date(Date.now()-60*60*1000));"/>
                <p>
                  <input type="button" onclick="punchIn(this.form)" value="In"/>
                  <xsl:text> </xsl:text>
                  <input type="button" onclick="punchOut(this.form)" value="Out"/>
                  <xsl:text> </xsl:text>
                  <input type="button" onclick="punchBreak(this.form, 10)" value="10min Break"/>
                  <xsl:text> </xsl:text>
                  <input type="button" onclick="punchBreak(this.form, 30)" value="30min Break"/>
                  <xsl:text> </xsl:text>
                  <input type="button" onclick="punchBreak(this.form, 60)" value="1h Break"/>
                </p>
                <p>
                  Now: 
                  <input type="button" onclick="punchIn(this.form, 0)" value="In"/>
                  <xsl:text> </xsl:text>
                  <input type="button" onclick="punchOut(this.form, 0)" value="Out"/>
                  <xsl:text> </xsl:text>
                  <input type="button" onclick="punchBreak(this.form, 10, 0)" value="10min Break"/>
                  <xsl:text> </xsl:text>
                  <input type="button" onclick="punchBreak(this.form, 30, 0)" value="30min Break"/>
                  <xsl:text> </xsl:text>
                  <input type="button" onclick="punchBreak(this.form, 60, 0)" value="1h Break"/>
                </p>
                <p>
                  10min ago: 
                  <input type="button" onclick="punchIn(this.form, 10)" value="In"/>
                  <xsl:text> </xsl:text>
                  <input type="button" onclick="punchOut(this.form, 10)" value="Out"/>
                  <xsl:text> </xsl:text>
                  <input type="button" onclick="punchBreak(this.form, 10, 10)" value="10min Break"/>
                </p>
                <p>
                  30min ago: 
                  <input type="button" onclick="punchIn(this.form, 30)" value="In"/>
                  <xsl:text> </xsl:text>
                  <input type="button" onclick="punchOut(this.form, 30)" value="Out"/>
                  <xsl:text> </xsl:text>
                  <input type="button" onclick="punchBreak(this.form, 30, 30)" value="30min Break"/>
                </p>
                <p>
                  1h ago: 
                  <input type="button" onclick="punchIn(this.form, 60)" value="In"/>
                  <xsl:text> </xsl:text>
                  <input type="button" onclick="punchOut(this.form, 60)" value="Out"/>
                  <xsl:text> </xsl:text>
                  <input type="button" onclick="punchBreak(this.form, 60, 60)" value="1h Break"/>
                </p>
              </form>
            </td>
          </tr>
        </table>
      </body>
    </html>
  </xsl:template>
  
  <xsl:template match="*">
  </xsl:template>
  
  <xsl:template match="log:log">
    <table border="0">
      <xsl:for-each select="*">
        <xsl:sort select="@time" order="descending"/>
        <tr>
          <td style="text-align: right">
            <xsl:apply-templates select="@time"/>
          </td>
          <td style="text-align: center">
            <xsl:text>&#160;&#160;</xsl:text>
            <xsl:apply-templates select="." mode="symbol"/>
            <xsl:text>&#160;&#160;</xsl:text>
          </td>
          <td>
            <xsl:apply-templates select="." mode="info"/>
          </td>
          <td>
            <a onclick="deleteLog('{@id}')">X</a>
          </td>
        </tr>
      </xsl:for-each>
    </table>
  </xsl:template>
  
  <xsl:template match="log:in" mode="symbol">
    <xsl:text>-></xsl:text>
  </xsl:template>
  
  <xsl:template match="log:out" mode="symbol">
    <xsl:text>&lt;-</xsl:text>
  </xsl:template>
  
  <xsl:template match="log:connect" mode="symbol">
    <xsl:text>>&lt;</xsl:text>
  </xsl:template>
  
  <xsl:template match="log:disconnect" mode="symbol">
    <xsl:text>&lt;></xsl:text>
  </xsl:template>
  
  <xsl:template match="log:break" mode="symbol">
    <xsl:text>...</xsl:text>
  </xsl:template>
  
  <xsl:template match="log:mood" mode="symbol">
    <xsl:choose>
      <xsl:when test="@type = 'happy'">:-)</xsl:when>
      <xsl:when test="@type = 'tired'">:()</xsl:when>
      <xsl:when test="@type = 'sad'">:-(</xsl:when>
      <xsl:when test="@type = 'confused'">:-S</xsl:when>
      <xsl:otherwise>-</xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <xsl:template match="log:in" mode="info">
    <xsl:apply-templates select="@ref"/>
  </xsl:template>
  
  <xsl:template match="log:out" mode="info">
    <xsl:apply-templates select="@ref"/>
  </xsl:template>
  
  <xsl:template match="log:connect" mode="info">
    <xsl:apply-templates select="@host"/>
  </xsl:template>
  
  <xsl:template match="log:disconnect" mode="info">
    <xsl:apply-templates select="@host"/>
  </xsl:template>
  
  <xsl:template match="log:break" mode="info">
    <xsl:value-of select="@duration"/>
    <xsl:text>min break</xsl:text>
  </xsl:template>
  
  <xsl:template match="log:mood" mode="info">
    <xsl:value-of select="@type"/>
  </xsl:template>
  
  <xsl:template match="@time">
    <xsl:value-of
      select="format-dateTime(xs:dateTime(.), '[FNn] [D01].[M01].[Y01] [H01]:[m01]:[s01]')"/>
  </xsl:template>
  
  <xsl:template match="@ref">
    <xsl:variable name="obj" select="substring-before(., '#')"/>
    <xsl:variable name="id" select="substring-after(., '#')"/>
    <xsl:apply-templates select="document($obj)//*[@id = $id]"/>
  </xsl:template>
  
  <xsl:template match="pm:task">
    <a onclick="document.getElementById('ref').value = '{$testTodoUri}#{@id}';">
      <xsl:value-of select="pm:title"/>
    </a>
  </xsl:template>
  
  <xsl:template match="pm:task" mode="list">
  	<a onclick="document.getElementById('ref').value = '{$testTodoUri}#{@id}';">
      <xsl:value-of select="pm:title"/>
    </a>
    <br/>
  </xsl:template>
  
</xsl:stylesheet>
